package com.tensqure.search.controller;

import com.tensqure.search.pojo.Article;
import com.tensqure.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * Created by 队长 on 2018/12/9.
 */

@RestController
@RequestMapping(value = "/article")
@CrossOrigin
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 添加索引库
     * @param article
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article){
        articleService.save(article);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    /**
     * 分页查询
     * @param key
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/{key}/{page}/{size}",method = RequestMethod.GET)
    public Result findAll(@PathVariable String key,@PathVariable int page,@PathVariable int size){
        Page<Article> page1 = articleService.findByKey(key,page,size);
        return  new Result(true,StatusCode.OK,"查询成功",new PageResult<Article>(page1.getTotalElements(),page1.getContent()));
    }

}
